package com.example.myapplication14_2;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private VideoView videoView;
    private Button btnPlay, btnStop;
    private MediaController mediaController;
    private String currentVideoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        videoView = findViewById(R.id.videoView);
        btnPlay = findViewById(R.id.btnPlay);
        btnStop = findViewById(R.id.btnStop);
        Spinner videoSpinner = findViewById(R.id.videoSpinner);

        String[] videoFiles = {"testvideo", "video1", "video2"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, videoFiles);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        videoSpinner.setAdapter(adapter);

        // 设置默认视频路径
        currentVideoPath = getVideoUri("testvideo");
        setupVideo(currentVideoPath);

        // Spinner 选择监听器
        videoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String videoName = videoFiles[position];
                String videoPath = getVideoUri(videoName);
                currentVideoPath = videoPath;
                setupVideo(videoPath);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing
            }
        });

        // 播放按钮点击事件
        btnPlay.setOnClickListener(v -> togglePlayPause());

        // 停止按钮点击事件
        btnStop.setOnClickListener(v -> {
            videoView.stopPlayback();
            setupVideo(currentVideoPath);
            updatePlayButtonState(); // 更新按钮状态
        });
    }

    /**
     * 构建 Android 资源 URI
     */
    private String getVideoUri(String videoName) {
        return "android.resource://" + getPackageName() + "/raw/" + videoName;
    }

    /**
     * 配置视频播放器
     */
    private void setupVideo(String videoPath) {
        videoView.stopPlayback(); // 释放当前视频资源

        videoView.setVideoURI(Uri.parse(videoPath));
        videoView.setMediaController(null);
        mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        mediaController.setAnchorView(videoView);

        videoView.setOnPreparedListener(mp -> {
            mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
            mp.setLooping(false); // 关闭循环播放
            runOnUiThread(this::updatePlayButtonState); // 切换视频后更新按钮状态
        });
    }

    /**
     * 切换播放/暂停状态
     */
    private void togglePlayPause() {
        if (videoView.isPlaying()) {
            videoView.pause();
            btnPlay.setText("播放");
        } else {
            videoView.start();
            btnPlay.setText("暂停");
        }
    }

    /**
     * 更新播放按钮状态
     */
    private void updatePlayButtonState() {
        if (videoView.isPlaying()) {
            btnPlay.setText("暂停");
        } else {
            btnPlay.setText("播放");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (videoView.isPlaying()) {
            videoView.pause();
            btnPlay.setText("播放");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.stopPlayback();
        }
    }
}